package com.zxl.grocery.microservice.auth.server.service.impl;

import com.zxl.grocery.microservice.api.user.UserInfo;
import com.zxl.grocery.microservice.auth.common.util.jwt.IJWTInfo;
import com.zxl.grocery.microservice.auth.common.util.jwt.JWTInfo;
import com.zxl.grocery.microservice.auth.server.feign.IUserService;
import com.zxl.grocery.microservice.auth.server.service.AuthService;
import com.zxl.grocery.microservice.auth.server.util.user.JwtTokenUtil;
import com.zxl.grocery.microservice.common.exception.auth.UserTokenException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * @Author: zxl
 * @Description:
 * @Date: Created in 10:20 2017/10/14
 * @Modified By:
 */
@Service
public class AuthServiceImpl implements AuthService {

    private JwtTokenUtil jwtTokenUtil;
    private IUserService userService;
//    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);//Spring Security 提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强哈希方法来加密密码。BCrypt强哈希方法 每次加密的结果都不一样，但是存贮其中一次加密结果 也能够验证成功

    @Autowired
    public AuthServiceImpl(
            JwtTokenUtil jwtTokenUtil,
            IUserService userService) {
        this.jwtTokenUtil = jwtTokenUtil;
        this.userService = userService;
    }

    @Override
    public String login(String username, String password) throws Exception {
//        UserInfo info = userService.getUserByUsername(username);//根据用户名获取用户信息,从grocery-admin服务获取
        UserInfo info = userService.validate(username,password);
        if (info == null){
            throw new UserTokenException("账户不存在！");
//            return "账户不存在！";
        }
        String token = "";
        //encoder.encode(user.getRawPassword().trim()) 加密
//        if (encoder.matches(password, info.getPassword())) {
        if (!StringUtils.isEmpty(info.getId())) {
//            密码验证通过，生成token
            token = jwtTokenUtil.generateToken(new JWTInfo(info.getUsername(), info.getId() + "", info.getName()));
        }else {
            throw new UserTokenException("密码错误！");
//            return "密码错误！";
        }
        return token;
    }

    /**
     * @Author: Lzx
     * @Description: 获取token中的存放的信息
     * @Params:  * @param null
     * @Date: 10:31 2017/10/19
     */
    @Override
    public IJWTInfo validate(String token) throws Exception {
        return jwtTokenUtil.getInfoFromToken(token);
    }

//    @Override
//    public FrontUser getUserInfo(String token) throws Exception {
//        String username = jwtTokenUtil.getInfoFromToken(token).getUniqueName();//获取token中的存放的账号
//        if (username == null) {
//            return null;
//        }
//        UserInfo user = userService.getUserByUsername(username);//根据账号获取用户的详细信息
//        if(user != null){
//            BaseContextHandler.setToken(token);
//        }
//        FrontUser frontUser = new FrontUser();
//        BeanUtils.copyProperties(user, frontUser);
//        List<PermissionInfo> permissionInfos = userService.getPermissionByUsername(username);
//        Stream<PermissionInfo> menus = permissionInfos.parallelStream().filter((permission) -> {
//            return permission.getType().equals(CommonConstants.RESOURCE_TYPE_MENU);
//        });
//        frontUser.setMenus(menus.collect(Collectors.toList()));
//        Stream<PermissionInfo> elements = permissionInfos.parallelStream().filter((permission) -> {
//            return !permission.getType().equals(CommonConstants.RESOURCE_TYPE_MENU);
//        });
//        frontUser.setElements(elements.collect(Collectors.toList()));
//        return frontUser;
//    }

    @Override
    public Boolean invalid(String token) {
        // TODO: 2017/9/11 注销token
        return null;
    }

    @Override
    public String refresh(String oldToken) {
        // TODO: 2017/9/11 刷新token
        return null;
    }
}
